package com.cqust.controller;

import com.cqust.entity.User;

import com.cqust.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
public class UserContorller {

    @Autowired
    private UserService userService;

    @GetMapping("/PersonalInfo")
    public String PersonalInfo(HttpServletRequest request ) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("USER");
        System.out.println(user+"个人信息");
        return "PersonalInfo";
    }
    @PostMapping("/PersonalInfo")
    public String updatePersonalInfo(User user,HttpServletRequest request) {
        System.out.println(user+"个人信息xg");
        if (user == null) {
            request.getSession().invalidate();
            return "redirect:index.jsp"; // 如果用户未登录，重定向到登录页面
        }
        userService.updateUser(user);
        request.getSession().invalidate();
    return "redirect:index.jsp"; // 重定向回个人信息页面

}


    @GetMapping("/main")
    public String Main()
    {
        return "main";
    }

    //注册请求
    @GetMapping("/register")
    public String redirect()
    {
        return "register";
    }
    @PostMapping("/register")//redirect 带不过去 forward可以带过去
    public String register(User user, HttpServletRequest req)
    {
        HttpSession session = req.getSession();
        System.out.println(user);
        if((userService.register(user))>0){
            session.setAttribute("registerMessage", "注册成功");
            return "redirect:index.jsp";
        }else{
            session.setAttribute("registerMessage", "注册失败");
            return "register";
        }

    }

    //登录请求
    @GetMapping("/login")
    public String login()
    {
        System.out.println("url..........");
        return "login";
    }

    @PostMapping("/login")
    public String login( User user,HttpServletRequest req) {
        HttpSession session = req.getSession();
        User user1 = userService.login(user);
        if (user1 == null) {
            System.out.println(user1);
            session.setAttribute("LoginMessage", "用户名或密码错误");
            return "redirect:index.jsp";
        }
        System.out.println(user1+"登录成功");
        session.setAttribute("USER", user1);
        return "main";
    }


    @RequestMapping("/logout")
    public String logout(HttpServletRequest req) {
        req.getSession().invalidate();
        return "redirect:index.jsp";
    }

//    @PostMapping("/deleteAccount")
//    public String deleteAccount(HttpServletRequest request) {
//    HttpSession session = request.getSession();
//    User user = (User) session.getAttribute("USER");
//    if (user == null) {
//        return "redirect:/index.jsp"; // 如果用户未登录，重定向到登录页面
//    }
//    int uId = user.getId();
//    // 调用服务层进行账户注销
//    if(userService.deleteUserById(uId)>0){
//        userService.deleteCardByUid(uId); // 删除用户银行卡信息
//    }
//    session.invalidate(); // 注销当前用户的会话
//    return "redirect:/index.jsp"; // 注销后重定向到登录页面
//}



//    @ResponseBody
//    @GetMapping("/listRecord")
//    public String userList(HttpServletRequest req) {
//
//        HttpSession session = req.getSession();
//
//        if(session.getAttribute("USER")!=null) {
//            User user = (User) session.getAttribute("USER");
//            List<Record> lst = bankService.getRecords(user.getuId());
//            System.out.println(lst);
//            return JSON.toJSONString(lst);
//        }else{
//            return "ERROR";
//        }
//    }

}
